package com.example.back.service.impl;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.example.back.dto.ExcelOrderRx;
import com.example.back.jpa.RxDrugsRepository;
import com.example.back.jpa.RxInfoRepository;
import com.example.back.mapper.RxMapper;
import com.example.back.service.RxService;
import com.example.back.utils.ExcelUtils;
import com.example.common.enums.DType;
import com.example.common.support.ApiException;
import com.example.common.entity.RxDrugs;
import com.example.common.entity.RxInfo;
import com.example.common.enums.ChkStatus;

import org.springframework.stereotype.Service;

import cn.hutool.core.date.DateUtil;

/**
 * @Author wxy
 * @Date 2021/1/13 14:43
 * @Version 1.0
 */
@Service
public class RxServiceImpl implements RxService {
    
    @Resource
    private RxMapper rxMapper;
    
    @Resource
    private RxDrugsRepository rxDrugsRepository;

    @Resource
    private RxInfoRepository rxInfoRepository;
        
    @Resource
    private ExcelUtils excelUtils;

    /**
     * 处方订单
     */
    @Override
    public Map<String, Object> List(String rxorderno,String orderno,String dName,String pName,String cName,byte bDType,int iChkStatus,int page,int limit) {
        int skip = (page - 1) * limit;
        if (skip < 0) skip = 0;

        List<Map<String, Object>> list = rxMapper.RxList(rxorderno, orderno, dName, pName, cName, bDType, iChkStatus, skip, limit);
        int count = rxMapper.RxListCount(rxorderno, orderno, dName, pName, cName, bDType, iChkStatus);
        Map<String, Object> res = new HashMap<>(); 
        list.forEach(m -> {
            m.put("DTypeName", DType.valueToDesc(((Long)m.get("DType")).intValue()));
            m.put("ChkStatus", ChkStatus.valueToDesc(((Long)m.get("ChkStatus")).intValue()));
            m.put("ChkTime", m.get("ChkTime") == null ? "/" : m.get("ChkTime"));
        });
        res.put("list", list);
        res.put("cnt", count);
        return res;
    }

    /**
     *
     */
    @Override
    public List<Map<String, Object>> OrderExportXls(String rxorderno,String orderno,String dName,String pName,String cName,byte bDType,int iChkStatus) {
        List<Map<String, Object>> list = rxMapper.RxList(rxorderno, orderno, dName, pName, cName, bDType, iChkStatus, 0, Integer.MAX_VALUE);
        List<ExcelOrderRx> olist = new ArrayList<>();           
        
        list.forEach(m -> {
            ExcelOrderRx excel = new ExcelOrderRx();
            excel.setRxOrderNO(m.get("RxOrderNO").toString());
            excel.setOrderNO(m.get("OrderNO").toString());
            excel.setCreateTime(m.get("CreateTime").toString());
            excel.setDepName(m.get("depName").toString());
            excel.setOpenDoctor(m.get("OpenDoctor").toString());
            excel.setName(m.get("Name").toString());
            excel.setTypeName(DType.valueToDesc(((Long)m.get("DType")).intValue()));
            excel.setChkStatus(ChkStatus.valueToDesc(((Long)m.get("ChkStatus")).intValue()));
            excel.setChkDoctor(m.get("ChkDoctor") != null ? m.get("ChkDoctor").toString() : "");
            excel.setChkTime(m.get("ChkTime") == null ? "/" : m.get("ChkTime").toString());
            olist.add(excel);
        });
        
        String now = DateUtil.now();
        String fileName =  "cash_" + now;
        try {
            excelUtils.excelExport(ExcelOrderRx.class,olist,fileName);
        }catch (Exception e){
            throw  new ApiException(1,e.getMessage());
        }
        
        return null;
    }

    /**
     * 处方列表
     */
    @Override
    public Map<String, Object> RxOrderList(String rxorderno) {
        Map<String, Object> rxobj = rxMapper.RxOrderDetail(rxorderno);

        Map<String, Object> zdetail = new HashMap<>();
        List<Map<String, Object>> xdetail = new ArrayList<>();
        if(((Number)rxobj.get("DType")).intValue() == 1){
            RxInfo zd = rxInfoRepository.GetzdList(rxorderno);
            zdetail.put("Avoid", zd.getAvoid());
            zdetail.put("MedTime", zd.getUseTime());
            zdetail.put("DrugInfo", zd.getDrugInfo());
            zdetail.put("TotalDoes", zd.getTotalDoes());
            zdetail.put("DayCount", zd.getDayCount());
            zdetail.put("Does", zd.getDoes());
            zdetail.put("MedWay", zd.getMedWay());
        } else if(((Number)rxobj.get("DType")).intValue() == 2){
            List<RxDrugs> xdList = rxDrugsRepository.GetxdList(rxorderno);
            xdList.forEach(m -> {
                Map<String, Object> o = new HashMap<>();
                o.put("DrugName", m.getDrugName());
                o.put("Spec", m.getSpec());
                o.put("Num", m.getNum());
                o.put("Suggest", m.getMedWay() + "；每日" + m.getFreq() + "；" + m.getDoes() + m.getDoesUnit() + "/次；" + m.getDay() + "天");
                o.put("Remark", m.getRemark());
                xdetail.add(o);
            });
        }

        Map<String, Object> res = new HashMap<>();
        res.put("RxOrderNO", rxobj.get("RxOrderNO"));
        res.put("OrderNO", rxobj.get("OrderNO"));
        res.put("DType", rxobj.get("DType"));
        res.put("DTypeName", DType.valueToDesc(((Long)rxobj.get("DType")).intValue()));
        res.put("CreateTime", rxobj.get("CreateTime"));
        res.put("depName", rxobj.get("depName"));
        res.put("OpenDoctor", rxobj.get("OpenDoctor"));
        res.put("ChkTime", rxobj.get("ChkTime") == null ? "/" : rxobj.get("ChkTime"));
        res.put("ChkStatus", rxobj.get("ChkStatus"));
        res.put("ChkStatusName", ChkStatus.valueToDesc(((Long)rxobj.get("ChkStatus")).intValue()));
        res.put("ChkDoctor", rxobj.get("ChkDoctor"));
        res.put("FailureReason", rxobj.get("FailureReason"));
        res.put("FailureRemark", rxobj.get("FailureRemark"));
        res.put("Name", rxobj.get("Name"));
        res.put("Sex", rxobj.get("Sex"));
        res.put("Age", rxobj.get("Age"));
        res.put("Dialectical", rxobj.get("Dialectical"));
        res.put("detail", ((Number)rxobj.get("DType")).intValue() == 1 ? zdetail : xdetail);
        return res;
    }
}
